<?php

namespace app\api\controller;

use app\common\model\Userinstore;
use app\common\model\Userinstorecount;
use app\common\model\Userreceivetic;
use app\admin\model\Storeuser as StoreuserMdl;
use app\admin\model\User;
use app\admin\model\Userconsumedetail;

use think\Db;
use think\Exception;

class Storeuser extends Base
{

    /**
     * 客户管理
     */
    public function getList(){
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;
        if (empty($param->storeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'a.storeId' => $param->storeId
        ];

        $model = new StoreuserMdl();
        $fields = 'a.userId, a.storeId, u.userName, u.imgUrl';
        $result = $model->alias('a')->where($where)->join('user u', 'a.userId = u.id')->field($fields)->limit($start, $length)->select();
        $total = $model->alias('a')->where($where)->join('user u', 'a.userId = u.id')->count();

        if (empty($result)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到客户');
        }

        foreach ($result as &$item){
            // 领券数量
            $item['countReceiveTicket'] = Userreceivetic::where(['userId' => $item['userId'], 'storeId' => $item['storeId']])->count();
            // 核销数量
            $item['countCancelRecord'] = Userreceivetic::where(['userId' => $item['userId'], 'storeId' => $item['storeId'], 'status' => 1])->count();
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 详情
     */
    public function getDetail(){
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'id 不能为空');
        }
        if (empty($param->storeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId 不能为空');
        }

        $userId = $param->id;
        $storeId = $param->storeId;
        $where = [
            'id' => $userId,
        ];

        $fields = '*';
        $item = User::where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.error_code'), self::NOT_DATA, '未查到客户');
        }

        //+++++ 最近到店 +++++//
        $userinstoreInfo = Userinstore::where(['userId' => $userId, 'storeId' => $storeId])->find();
        // 累计进店
        $totalInStore = $userinstoreInfo['inStoreCount'];
        // 最近1周进店
        $weekInStore = Userinstorecount::where(['userId' => $userId, 'storeId' => $storeId])->whereTime('inStoreTime', 'week')->count();
        // 最近一次进店时间
        $lastInStoreTime = $userinstoreInfo['inStoreTime'];
        $lateInStore = [
            'totalInStore' => $totalInStore ? $totalInStore : 0,
            'weekInStore' => $weekInStore ? $weekInStore : 0,
            'lastInStoreTime' => $lastInStoreTime ? $lastInStoreTime : ''
        ];
        $item['lateInStore'] = $lateInStore;

        //+++++ 最近领券 +++++//
        // 累计领券
        $totalReceiveTicket = Userreceivetic::where(['userId' => $userId, 'storeId' => $storeId])->count();
        // 最近1周领券
        $weekReceiveTicket = Userreceivetic::where(['userId' => $userId, 'storeId' => $storeId])->whereTime('receiveTicketTime', 'week')->count();
        // 最近一次领券时间
        $lastReceiveTicketTime = Userreceivetic::where(['userId' => $userId, 'storeId' => $storeId])->order('receiveTicketTime DESC')->value('receiveTicketTime');
        $lateReceiveTicket = [
            'totalReceiveTicket' => $totalReceiveTicket ? $totalReceiveTicket : 0,
            'weekReceiveTicket' => $weekReceiveTicket ? $weekReceiveTicket : 0,
            'lastReceiveTicketTime' => $lastReceiveTicketTime ? $lastReceiveTicketTime : ''
        ];
        $item['lateReceiveTicket'] = $lateReceiveTicket;

        //+++++ 最近消费 +++++//
        // 累计消费
        $totalConsume = Userconsumedetail::where(['userId' => $userId, 'storeId' => $storeId])->count();
        // 最近1周消费
        $weekConsume = Userconsumedetail::where(['userId' => $userId, 'storeId' => $storeId])->whereTime('payTime', 'week')->count();
        // 最近一次消费时间
        $lastConsumeTime = Userconsumedetail::where(['userId' => $userId, 'storeId' => $storeId])->order('payTime DESC')->value('payTime');
        $lateConsume = [
            'totalConsume' => $totalConsume ? $totalConsume : 0,
            'weekConsume' => $weekConsume ? $weekConsume : 0,
            'lastConsumeTime' => $lastConsumeTime ? $lastConsumeTime : ''
        ];
        $item['lateConsume'] = $lateConsume;

        return show(config('status.success_code'), self::MSG_SUCCESS, $item);
    }

}